package top.itaso.app.ui.progressbar;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import top.itaso.app.R;
import top.itaso.lib.alog.ALog;
import top.itaso.lib.widget.DownloadProgressBarView1;
import top.itaso.lib.widget.DownloadProgressBarView2;
import top.itaso.lib.widget.WaterWaveLoadingView;

public class ProgressBarFragment extends Fragment implements DownloadProgressBarView2.ICloseCallback {
    private static final String TAG = "ProgressBarFragment";
    private ProgressBarViewModel viewModel;

    private Button btnStartUpload;

    //进度条示例1
    private TextView pb1CurrentSize;
    private TextView pb1CurrentSpeed;
    private DownloadProgressBarView1 downloadProgressBarView1;

    //进度条示例2
    private DownloadProgressBarView2 downloadProgressBarView2;

    //帧动画：animation-list
    private ImageView mIvUploading;
    private AnimationDrawable mUploadingAnimDrawable;
    private ImageView mIvDownloading;
    private AnimationDrawable mDownloadingAnimDrawable;

    private WaterWaveLoadingView mWaterWaveLoadingView;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_download_progressbar, container, false);
        initView(rootView);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewModel = new ViewModelProvider(this).get(ProgressBarViewModel.class);
        getLifecycle().addObserver(viewModel);
        viewModel.observeDownloadFile().observe(getViewLifecycleOwner(), new Observer<DownloadFileBean>() {
            @Override
            public void onChanged(DownloadFileBean downloadFileBean) {
                ALog.vTag(TAG, "fileState:%s,download:%s,DownloadPercentValue:%s", downloadFileBean.getFileState().name(), downloadFileBean.getDownloadSize(), downloadFileBean.getHasDownloadPercentValue());

                if (downloadFileBean.getFileState() == DownloadFileBean.EFileState.STARTING) {
                    btnStartUpload.setText("点击暂停下载/上传");
                } else {
                    btnStartUpload.setText("点击开始下载/上传");
                }

                //进度条示例1
                downloadProgressBarView1.setMaxValue(downloadFileBean.getFileSize());
                downloadProgressBarView1.setCurrentValue(downloadFileBean.getDownloadSize());
                pb1CurrentSize.setText(
                        String.format(getResources().getString(R.string.string_progressbar_current_size),
                                Formatter.formatFileSize(getContext(), downloadFileBean.getDownloadSize()),
                                "/",
                                Formatter.formatFileSize(getContext(), downloadFileBean.getFileSize())
                        )
                );
                pb1CurrentSpeed.setText(Formatter.formatFileSize(getContext(), downloadFileBean.getDownloadSpeed()) + "/s");

                //进度条示例2
                downloadProgressBarView2.refreshProgress(downloadFileBean.getSavePath(), downloadFileBean.getHasDownloadPercentValue());

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ALog.d("onDestroyView......");
    }


    @Override
    public void onDetach() {
        super.onDetach();
        ALog.d("onDetach......");
        viewModel.onInterruptDownload(DownloadFileBean.EFileState.STOPED);
    }

    @Override
    public void onInterrupt() {
        ALog.d("上传中断");
        viewModel.onInterruptDownload(DownloadFileBean.EFileState.PAUSING);
    }

    private void initView(View rootView) {
        btnStartUpload = rootView.findViewById(R.id.btn_start_progress);
        btnStartUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.startDownload();
            }
        });

        initViewPb1(rootView.findViewById(R.id.layout_download_progressbar_1));

        initViewPb2(rootView.findViewById(R.id.layout_download_progressbar_2));

        mIvUploading = rootView.findViewById(R.id.iv_upload_anim);
        mUploadingAnimDrawable = (AnimationDrawable) mIvUploading.getDrawable();//获取 androd:src 设置的帧动画资源
        mIvUploading.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mUploadingAnimDrawable == null) {
                    return;
                }
                if (!mUploadingAnimDrawable.isRunning()) {
                    mUploadingAnimDrawable.start();
                } else {
                    mUploadingAnimDrawable.stop();
                }
            }
        });

        mIvDownloading = rootView.findViewById(R.id.iv_download_anim);
        mDownloadingAnimDrawable = (AnimationDrawable) mIvDownloading.getBackground();//获取 androd:background 设置的帧动画资源
        mIvDownloading.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDownloadingAnimDrawable == null) {
                    return;
                }
                if (!mDownloadingAnimDrawable.isRunning()) {
                    mDownloadingAnimDrawable.start();
                } else {
                    mDownloadingAnimDrawable.stop();
                }
            }
        });

        initWaterWave(rootView);
    }

    private void initViewPb2(View layoutProgressbarView) {
        downloadProgressBarView2 = layoutProgressbarView.findViewById(R.id.layout_download_progressbar_2);
        downloadProgressBarView2.setCloseCallback(this);
    }

    private void initViewPb1(View layoutProgressbarView) {
        pb1CurrentSize = layoutProgressbarView.findViewById(R.id.tv_current_size);
        pb1CurrentSize.setText(String.format(getResources().getString(R.string.string_progressbar_current_size), "0MB", "/", "0MB"));

        pb1CurrentSpeed = layoutProgressbarView.findViewById(R.id.tv_current_speed);
        pb1CurrentSpeed.setText("0MB/s");

        downloadProgressBarView1 = layoutProgressbarView.findViewById(R.id.widget_pbv_1);
    }

    private void initWaterWave(View rootView) {
        mWaterWaveLoadingView = rootView.findViewById(R.id.layout_water_wave);
    }


}